package com.xmg.client.mgrsite.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xmg.shop.api.domain.*;
import com.xmg.shop.api.service.IProductService;
import com.xmg.shop.api.service.IProductSkuService;
import com.xmg.shop.api.service.ISkuPropertyService;
import com.xmg.shop.api.service.ISkuPropertyValueService;
import com.xmg.shop.api.util.AjaxResult;
import com.xmg.shop.api.vo.ProductSkuVo;
import com.xmg.shop.api.vo.SkuGenerateFormVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by KavenC on 2017/11/15.
 */
@Controller
public class ProductSkuController {

    @Reference
    private IProductSkuService productSkuService;
    @Reference
    private IProductService productService;
    @Reference
    private ISkuPropertyService skuPropertyService;
    @Reference
    private ISkuPropertyValueService skuPropertyValueService;

    /**
     *sku跳转页面
     */
    @RequestMapping("/productSku/{productId}")
    public String productSku(@PathVariable("productId") Long productId, Model model){
        //接收页面传过来的productId,
        int count = productSkuService.queryCountByProductId(productId);
        //再判断该商品对象之前是否生成过sku,如果生成过sku的话,就放回一个sku的管理界面,在该界面上实现crud的功能,
        if(count>0){
            //sku集合
            List<ProductSku> productSkuList = productSkuService.selectSkuByProudct(productId);
            model.addAttribute("productSkuList",productSkuList);
            //遍历sku的属性,获取属性名
            ArrayList<String> skuPropertyList = new ArrayList<String>();
            List<ProductSkuProperty> productSkuPropertyList = productSkuList.get(0).getProductSkuPropertyList();
            for (ProductSkuProperty productSkuProperty : productSkuPropertyList) {
                skuPropertyList.add(productSkuProperty.getSkuProperty().getName());
            }

            model.addAttribute("skuPropertyList",skuPropertyList);

            return "product/sku_manage";
        }else{
        //如果没有生成过sku,返回一个生成sku的界面.
            Product product = productService.get(productId);
            Long catalogId = product.getCatalog().getId();
            List<SkuProperty> skuPropertyList = skuPropertyService.selectByCatalogId(catalogId);
            model.addAttribute("product",product);
            model.addAttribute("skuPropertyList",skuPropertyList);
            return "product/sku_generate";
        }
    }

    /**
     *添加sku属性栏的请求地址
     */
    @RequestMapping("/productSku/skuPropertyValue/{skuPropertyId}")
    public String skuPropertyValue(@PathVariable("skuPropertyId") Long skuPropertyId,Model model){
        //根据id查出skuProperty
        SkuProperty skuProperty = skuPropertyService.selectByskuPropertyId(skuPropertyId);

        model.addAttribute("skuProperty",skuProperty);

        //根据skuPropertyId查出SkuPropertyValue
        List<SkuPropertyValue> skuPropertyValue = skuPropertyValueService.selectByskuPropertyId(skuPropertyId);

        model.addAttribute("skuPropertyValue",skuPropertyValue);

        return "product/sku_property_value_table";
    }

    /**
     * 返回生成sku的html页面
     * @return
     */
    @RequestMapping("/productSku/generateSku")
    public String generateSku(@RequestBody SkuGenerateFormVo vo, Model model){
        List<Map<String,Object>> skuList = productSkuService.generateSku(vo);
        model.addAttribute("skuList", skuList);
        model.addAttribute("skuPropertieList", vo.getSkuPropertieList());
        return "product/sku_form";
    }

    /**
     * 保存sku
     */
    @RequestMapping("/productSku/save")
    @ResponseBody
    public AjaxResult save(ProductSkuVo productSkuVo){

        AjaxResult ajaxResult = null;
        try {
            productSkuService.save(productSkuVo);
            ajaxResult = new AjaxResult(true,"保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = new AjaxResult(false, "保存失败!"+e.getMessage());
        }

        return ajaxResult;
    }

}
